#include "Player.h"

#include "Team.h"

#include <sstream>

namespace OOInterface
{

Player::Player( int id ):
	_id( id )
{
}

Player::~Player()
{
}

void Player::changeTeam( ConstTeamPtr team )
{
	_team = team;
}

ConstTeamPtr Player::getTeam() const
{
	return _team;
}

bool Player::isAllyOf( ConstPlayerPtr p ) const
{
	return _team->has( p );
}

bool Player::isEnemyOf( ConstPlayerPtr p ) const
{
	return !_team->has( p );
}

ConstUnitSet Player::getUnits() const
{
	return _units;
}

void Player::addUnit( ConstUnitPtr unit )
{
	_units.insert( unit );
}

void Player::removeUnit( ConstUnitPtr unit )
{
	_units.erase( unit );
}

float Player::getResourceAmount( const Resource& r ) const
{
	ResourceMap::const_iterator i = _resources.find( r );
	return i != _resources.end() ? (*i).second : 0.0f;
}

void Player::setResourceAmount( const Resource& r , float amt )
{
	_resources.erase( r );
	_resources.insert( std::pair< const Resource, float>( r, amt ) );
}

float Player::getResourceStorage( const Resource& r ) const
{
	ResourceMap::const_iterator i = _resourceStorage.find( r );
	return i != _resourceStorage.end() ? (*i).second : 0.0f;
}

void Player::setResourceStorage( const Resource& r, float storage )
{
	_resourceStorage.erase( r );
	_resourceStorage.insert( std::pair< const Resource, float >( r, storage ));
}

float Player::getResourceUsage( const Resource& r ) const
{
	ResourceMap::const_iterator i = _resourceUsage.find( r );
	return i != _resourceUsage.end() ? (*i).second : 0.0f;
}

void Player::setResourceUsage( const Resource& r, float usage )
{
	_resourceUsage.erase( r );
	_resourceUsage.insert( std::pair< const Resource, float >( r, usage ) );
}

float Player::getResourceIncome( const Resource& r ) const
{
	ResourceMap::const_iterator i = _resourceIncome.find( r );
	return i != _resourceIncome.end() ? (*i).second : 0.0f;
}

void Player::setResourceIncome( const Resource& r, float usage )
{
	_resourceIncome.erase( r );
	_resourceIncome.insert( std::pair< const Resource, float >( r, usage ) );
}

std::string Player::toString() const
{
	std::stringstream ss;
	ss << "Player(" << _id << ") " << _faction.toString() << " Units owned: " << _units.size();
	return ss.str();
}

}
